#include "pm/workrequest.h"

using namespace pm;

const wxString WorkRequest::TABLE_NAME   = wxT("work_requests");

WorkRequest::WorkRequest()
: db::Object()
, m_amount(0)
, m_idProject(db::Object::undefined)
{
}

WorkRequest::WorkRequest(wxSQLite3ResultSet& res)
: db::Object(res)
, m_name(     res.GetString(wxT("name")))
, m_amount(   res.GetDouble(wxT("amount")))
, m_date(     res.GetDate  (wxT("date")))
, m_idProject(res.GetInt   (wxT("id_project"), db::Object::undefined))
{
}

db::update_value_type WorkRequest::prepare_insert(wxSQLite3Database& db)
{
	if (m_idProject == db::Object::undefined) {
		return db::update_value_type(false, wxSQLite3Statement());
	}
	
	wxSQLite3Statement stmt = db.PrepareStatement("insert into work_requests values(null, ?, ?, ?, ?)");
	stmt.Bind(1, m_name);
	stmt.Bind(2, m_amount);
	stmt.BindDate(3, m_date);
	stmt.Bind(4, m_idProject);
	return db::update_value_type(true, stmt);
}

db::update_value_type WorkRequest::prepare_update(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement("update work_requests set name = ?, amount = ?, date = ?, id_project = ? where id = ?");
	stmt.Bind(1, m_name);
	stmt.Bind(2, m_amount);
	stmt.BindDate(3, m_date);
	stmt.Bind(4, m_idProject);
	stmt.Bind(5, id());
	return db::update_value_type(true, stmt);
}


GetWorkRequestsByProject::GetWorkRequestsByProject(int id_project)
: m_idProject(id_project)
{
}

wxSQLite3Statement GetWorkRequestsByProject::prepare(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement("select id, name, amount, date, id_project from work_requests where id_project = ?");
	stmt.Bind(1, m_idProject);
	return stmt;
}
